package mytaxis.move.app;

import mytaxis.move.R;
import mytaxis.move.map.DriverMap;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;

public class RequestReceivedActivity extends Activity {

	private TextView num;
	private Thread countdown;
	private Button yes;
	private Button no;
	private MyTaxisService mBoundService;
	private boolean stop_thread = false;
	private boolean calledFromMap = false;
	private boolean requestTreated = false;
	
	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
			mBoundService = ((MyTaxisService.LocalBinder)service).getService();
		}

		public void onServiceDisconnected(ComponentName className) {
			mBoundService = null;
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		this.setContentView(R.layout.popup);
		
		Bundle b = getIntent().getExtras();
		
		if(b!=null && b.getString("isMap").equals("1")) calledFromMap = true;

		num = (TextView) findViewById(R.id.numPopup);
		num.setText("30");
		yes = (Button) findViewById(R.id.yesPopup);
		no = (Button) findViewById(R.id.noPopup);
		
		bindService(new Intent(this, MyTaxisService.class), mConnection, Context.BIND_AUTO_CREATE);	

		yes.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(mBoundService.acceptRequest(true,"-1")){
					stop_thread = true;
					Toast.makeText(getApplicationContext(),R.string.req_accepted,0).show();
					Bundle b = mBoundService.getRequestData();
					if(calledFromMap){
						Intent i = getIntent();
						i.putExtras(b);
						setResult(Activity.RESULT_OK, i);
					}
					else{
						Intent i = new Intent(RequestReceivedActivity.this,DriverMap.class);
						if(b!=null) i.putExtras(b);
						startActivity(i);
					}
					requestTreated=true;
					finish();
				}
				else{
					Toast.makeText(getApplicationContext(),R.string.connection_error,0).show();
				}
			}
		});

		no.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(mBoundService.acceptRequest(false,"-1")){
					stop_thread = true;
					Toast.makeText(getApplicationContext(),R.string.req_rejected,0).show();
					if(calledFromMap) setResult(Activity.RESULT_CANCELED);
					requestTreated=true;
					finish();
				}
				else{
					Toast.makeText(getApplicationContext(),R.string.connection_error,0).show();
				}
			}
		});

		countdown = new Thread(new Runnable() {
			public void run() {
				int i = 30;
				while (i > 0 && !stop_thread) {
					try {
						Thread.sleep(999);
						i--;
						Message msg = new Message();
						Bundle b = new Bundle();
						b.putString("num", String.valueOf(i));
						msg.setData(b);
						mHandler.sendMessage(msg);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}

			}
		});
		countdown.start();
	}
	
	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			String numMsg = msg.getData().getString("num");
			if (numMsg.equals("0")){
				mBoundService.acceptRequest(false,"-1");
				Toast.makeText(getApplicationContext(),R.string.req_rejected,0).show();
				requestTreated=true;
				finish();
			}
			else {
				if (numMsg.equals("9"))
					num.setTextColor(Color.parseColor("#FF0000"));
				num.setText(numMsg);
			}
		};
	};
	
	private BroadcastReceiver myReceiverSol = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle extras = intent.getExtras();				
			String data = extras.getString(MyTaxisService.IMM_REQ_DATA);
			if(data.equals("0")){
				Toast.makeText(getApplicationContext(),R.string.client_canceled,0).show();
				finish();
			}
		}
	};
	
	private void doRegister(){
		IntentFilter filter = new IntentFilter(MyTaxisService.IMM_REQ_FILTER);
		registerReceiver(myReceiverSol, filter);
	}

	private void doUnRegister(){
		unregisterReceiver(myReceiverSol);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		doRegister();
	}

	@Override
	protected void onPause() {
		super.onPause();
		doUnRegister();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(!requestTreated){
			mBoundService.acceptRequest(false,"-1");
			Toast.makeText(getApplicationContext(),R.string.req_rejected,0).show();
		}
		unbindService(mConnection);
	}
}
